<template>
  <div class="login df aic jcc fdc">
    <h1 class="title df aic jcc">系统登录</h1>
    <el-form ref="formRef" status-icon :rules="rules" :model="formData">
      <el-form-item prop="account">
        <el-input
          placeholder="请输入账号"
          prefix-icon="iconfont icon-user"
          v-model="formData.account"
          show-account
        ></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          @keyup.native.enter="submit"
          :type="flag ? 'text' : 'password'"
          placeholder="请输入密码"
          prefix-icon="iconfont icon-lock"
          v-model="formData.password"
        >
          <i
            @click="flag = !flag"
            slot="suffix"
            class="iconfont"
            :class="flag ? 'icon-eye-open' : 'icon-eye-close'"
          ></i>
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="submit" type="primary" class="login-btn">
          登录
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {mapActions} from 'vuex'
export default {
  data() {
    return {
      // 数据
      formData: {
        account: '',
        password: '',
      },
      //   眼睛
      flag: false,
      rules: {
        account: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur'},
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 3, max: 12, message: '长度在 3 到 12 个字符', trigger: 'blur'},
        ],
      },
    }
  },
  methods: {
    ...mapActions('user', ['A_login']),
    submit() {
      this.$refs.formRef.validate(async valid => {
        if (valid) {
          // 发送请求
          await this.A_login(this.formData)
          this.$router.push('/home')
        } else {
          console.log('发送失败')
        }
      })
    },
  },
}
</script>

<style lang="scss" scoped>
.login {
  width: 100%;
  height: 100%;
  background-color: $menuBg;
  .title {
    font-size: 20px;
    font-weight: bold;
    color: #eee;
    margin-bottom: 20px;
  }
  .el-input,
  .login-btn,
  .title {
    width: 400px;
    height: 40px;
  }
  // 深度穿透
  :deep(.el-input__inner) {
    background-color: $menuBg;
    color: #fff;
  }
  .icon-eye-close:before,
  .icon-eye-open:before {
    line-height: 40px;
  }
}
</style>
